package com.wz.app.http;

import java.util.List;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ListView;

import com.baidu.apistore.sdk.ApiCallBack;
import com.baidu.apistore.sdk.ApiStoreSDK;
import com.baidu.apistore.sdk.network.Parameters;
import com.wz.app.adapter.NewsAdapter;
import com.wz.app.model.Contentlist;
import com.wz.app.model.NewsRoot;
import com.wz.app.model.Pagebean;
import com.wz.app.model.Showapi_res_body;
import com.wz.app.utils.JsonTool;

public class GetJson  {

	private String url;
	private Parameters para;
	private Context context;
	private ListView listView;

	private NewsRoot newsRoot;
	private Showapi_res_body res_body;
	private Pagebean pb;
	private List<Contentlist> content;

	public GetJson(String url, Parameters para, Context context,
			ListView listView) {
		super();
		this.url = url;
		this.para = para;
		this.context = context;
		this.listView = listView;
	}

	public String get( ) {
		// TODO Auto-generated method stub
		para = new Parameters();
		para.put("channelId", "5572a109b3cdc86cf39001db");
		ApiStoreSDK.execute(url, ApiStoreSDK.GET, para, new ApiCallBack() {
			@Override
			public void onSuccess(int status, String responseString) {
				Log.i("sdkdemo", "onSuccess");
				// 获取json 数据类型 data
				String data = responseString;
				// 再次解析json
				newsRoot = new JsonTool().getNews(data);
				if (newsRoot != null) {
					res_body = newsRoot.getShowapi_res_body();
					pb = res_body.getPagebean();
					content = pb.getContentlist();
					
					NewsAdapter adapter = new NewsAdapter(content, context);
					listView.setAdapter(adapter);
				}
			}

			@Override
			public void onComplete() {
				Log.i("sdkdemo", "onComplete");
			}

			@Override
			public void onError(int status, String responseString, Exception e) {
				Log.i("sdkdemo", "onError, status: " + status);
				Log.i("sdkdemo", "errMsg: " + (e == null ? "" : e.getMessage()));
			}

			//
		});
		return null;
	}

	String getStackTrace(Throwable e) {
		if (e == null) {
			return "";
		}
		StringBuilder str = new StringBuilder();
		str.append(e.getMessage()).append("\n");
		for (int i = 0; i < e.getStackTrace().length; i++) {
			str.append(e.getStackTrace()[i]).append("\n");
		}
		return str.toString();
	}


}
